home *** CD-ROM | disk | FTP | other *** search
- Path: news.itsyd.bhp.com.au!news
- From: Richard Bowen <bowen.richard.rw@bhp.com.au>
- Newsgroups: comp.lang.c++
- Subject: Re: Operator overload problem!
- Date: Wed, 20 Mar 1996 09:50:42 +1000
- Organization: BHP IT
- Message-ID: <314F4852.3A75@bhp.com.au>
- References: <DoIn1z.Gou@latcs1.lat.oz.au>
- NNTP-Posting-Host: 134.18.154.118
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- The compiler doesn't know that ostream is a class. You need the
- include
-
- #include "iostream.h"
-
- At the top of the file.
-
-
- Richard
-
-
-
-
-
-
- Gregary J Boyles wrote:
- >
- > I am trying to overload the << but I get the syntax errors : friends must be functions or
- > classes, 'ostream' cannot start a parameter declaration, 'Address::operator <<(int &,Address &)'
- > must be declared at the first set of astericks and declaration syntax error at the second set.
- >
- > What the #$%& is it on about? I copied the operator overload function directly out of a program
- > which compiles and runs so why all of a sudden won't the bloody compiler accept it?
- >
- > #include "defines.h"
- >
- > class Address
- > {
- > private : int Number;
- > char *Street;
- > char *City;
- > int Zip;
- >
- > public : // Constructors
- > Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
- > Address();
- > // Copy constructor
- > Address(Address& AnAddress);
- > // Deconstructor
- > ~Address();
- > // Operator overloads
- >
- > **************************************************************************
- > * *
- > * friend ostream& operator <<(ostream& OutPutStream,Address& AnAddress); *
- > * *
- > **************************************************************************
- >
- > // Functions
- > void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
- > };
- >
- > #endif
- >
- > // address.cpp
- >
- > #include "address.h"
- > #include <string.h>
- >
- > // Constructors
- > Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- > {
- > Number=ANumber;
- > Street=new char[strlen(AStreet)+1];
- > strcpy(Street,AStreet);
- > City=new char[strlen(ACity)+1];
- > strcpy(City,ACity);
- > Zip=AZip;
- > }
- >
- > Address::Address()
- > {
- > Number=0;
- > Street=new char[strlen("")+1];
- > strcpy(Street,"");
- > City=new char[strlen("")+1];
- > strcpy(City,"");
- > Zip=0;
- > }
- >
- > // Copy constructor
- > Address::Address(Address& AnAddress)
- > {
- > Number=AnAddress.Number;
- > Street=new char[strlen(AnAddress.Street)+1];
- > strcpy(Street,AnAddress.Street);
- > City=new char[strlen(AnAddress.City)+1];
- > strcpy(City,AnAddress.City);
- > Zip=AnAddress.Zip;
- > }
- >
- > // Deconstructor
- > Address::~Address()
- > {
- > Number=0;
- > strcpy(Street,"");
- > strcpy(City,"");
- > Zip=0;
- > }
- >
- > // Operator overloads
- >
- > ***************************************************************
- > *
- > ostream& operator <<(ostream& OutPutStream,Address& AnAddress)*
- > *
- > ***************************************************************
- >
- > {
- > OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
- > OutPutStream<<"City : "<<AnAddress.City<<EOLN;
- > OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
- > }
- >
- > // Functions
- > void Address::Change(int ANumber,const char *AStreet,const char *ACity,int AZip)
- > {
- > Number=ANumber;
- > delete Street;
- > Street=new char[strlen(AStreet)+1];
- > strcpy(Street,AStreet);
- > delete City;
- > City=new char[strlen(ACity)+1];
- > strcpy(City,ACity);
- > Zip=AZip;
- > }
-